package org.nokatag.tagjava;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.noka.gson.Gson;
import org.nokatag.system.BugInfoWrint;

public class FormHtml extends Inputsuper{

	private static final long serialVersionUID = -4565995599173250865L;
	private String action=null;
	private String method="post";
	private String target="_self";
	private String subfunction=null;
	private String onsuccess=null;
	private String onfailure=null;
	private String onsubmit=null;
	private String style="";//style
	private String classstyle=""; //classstyle
	private String labeldefaultwidth = null;//默认宽度
	private String labeldefaultheight= null;//默认高度
	private String inputdefaultwidth = null;//默认宽度
	private String inputdefaultheight= null;//默认高度
	private String stopinfo = "false";//禁用消息提示
	private String nosubmit = null;//不提交标识
	private String divform = null;//是否以div方式加载form
	private String altform = null;//附加表单id,形如xx,rrr
	
	private Map<String, HashSet<InputItem>> ids = new HashMap<String,HashSet<InputItem>>();//ids
	
	public int doStartTag() throws JspException {
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		StringBuilder bodystr = new StringBuilder();
		bodystr.append(potjs(request));
		bodystr.append(nkUtiljs(request));
		bodystr.append(nkBoxJsAndCss(request));
		if("true".equalsIgnoreCase(divform)){
			bodystr.append("<div "+getBaseStr()+">");
		}else{
			bodystr.append("<form action=\""+(action==null?"":action)+"\" method=\""+method+"\" target=\""+target+"\" "+getBaseStr()+">");
		}
		bodystr.append("<div id=\""+getId()+"_formbody\">");
		bodystr.append("<div id=\""+getId()+"_jsbody_0\" style=\""+style+"\" class=\""+classstyle+"\">");
		try {
			jspOut.write(bodystr.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	public int doEndTag() throws JspException {
		try {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			StringBuilder bodystr = new StringBuilder();
			String basePath = basepath(request);
			bodystr.append("</div>");
			bodystr.append("</div>");
			if("true".equalsIgnoreCase(divform)){
				bodystr.append("</div>");
			}else{
				bodystr.append("</form>");
			}
			bodystr.append("<script type=\"text/javascript\">");
			bodystr.append("Event.observe(document, 'dom:loaded', function() {");
			bodystr.append("var nk_"+getId()+"_option ={");
			bodystr.append("target : '"+getId()+"',");
			if(onsubmit!=null)
				bodystr.append("onsubmit : function(){return "+onsubmit+"();},");
			if(onsuccess!=null)
				bodystr.append("onSuccess : function(response){"+onsuccess+"(response);},");
			if(onfailure!=null)
				bodystr.append("onFailure : function(response){"+onfailure+"(response);},");
			if(null!=stopinfo){
				bodystr.append("stopinfo : "+("true").equals(stopinfo)+",");
			}
			if(null!=nosubmit){
				bodystr.append("nosubmit : '"+nosubmit+"',");
			}
			if(null!=ids && !ids.isEmpty() && null!=ids.get(getId())){
				Gson gson = new Gson();
				bodystr.append("nkids :"+gson.toJson(ids.get(getId()))+",");
			}
			if(null!=altform){
				bodystr.append("altform : ['"+altform.replace(",", "','")+"'],");
			}
			bodystr.append("rurl : '"+basePath+"'");
			bodystr.append("};");
			bodystr.append("var nk_"+getId()+"_formObject = new NKFormHtml(nk_"+getId()+"_option);");
			bodystr.append("nk_"+getId()+"_formObject.show();");
			//--------------infobox------------------------------------------------------------------
			bodystr.append("});");
			if(null!=subfunction){//提交方法重命名
				bodystr.append("function "+subfunction+"(){");
				bodystr.append("$('"+getId()+"').ajaxSubmit();");
				bodystr.append("};");
			}
			bodystr.append("</script>");
			bodystr.append("<div style=\"clear:both;\"></div>");
			pageContext.getOut().write(bodystr.toString());
		} catch (IOException e) {
				BugInfoWrint.Print(e);
		}
		return EVAL_PAGE;
	}
	public void insertidx(String id,String name,String value){
		if(null==ids.get(getId())){
			ids.put(getId(), new HashSet<InputItem>());
		}
		ids.get(getId()).add(new InputItem(id,name,value));
	}
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public String getSubfunction() {
		return subfunction;
	}
	public void setSubfunction(String subfunction) {
		this.subfunction = subfunction;
	}
	public String getOnsuccess() {
		return onsuccess;
	}
	public void setOnsuccess(String onsuccess) {
		this.onsuccess = onsuccess;
	}
	public String getOnfailure() {
		return onfailure;
	}
	public void setOnfailure(String onfailure) {
		this.onfailure = onfailure;
	}
	public String getOnsubmit() {
		return onsubmit;
	}
	public void setOnsubmit(String onsubmit) {
		this.onsubmit = onsubmit;
	}
	public String getStyle() {
		return style;
	}
	public void setStyle(String style) {
		this.style = style;
	}
	public String getClassstyle() {
		return classstyle;
	}
	public void setClassstyle(String classstyle) {
		this.classstyle = classstyle;
	}
	public String getLabeldefaultwidth() {
		return labeldefaultwidth;
	}
	public void setLabeldefaultwidth(String labeldefaultwidth) {
		this.labeldefaultwidth = labeldefaultwidth;
	}
	public String getLabeldefaultheight() {
		return labeldefaultheight;
	}
	public void setLabeldefaultheight(String labeldefaultheight) {
		this.labeldefaultheight = labeldefaultheight;
	}
	public String getInputdefaultwidth() {
		return inputdefaultwidth;
	}
	public void setInputdefaultwidth(String inputdefaultwidth) {
		this.inputdefaultwidth = inputdefaultwidth;
	}
	public String getInputdefaultheight() {
		return inputdefaultheight;
	}
	public void setInputdefaultheight(String inputdefaultheight) {
		this.inputdefaultheight = inputdefaultheight;
	}
	public String getStopinfo() {
		return stopinfo;
	}
	public void setStopinfo(String stopinfo) {
		this.stopinfo = stopinfo;
	}
	public String getNosubmit() {
		return nosubmit;
	}
	public void setNosubmit(String nosubmit) {
		this.nosubmit = nosubmit;
	}
	public String getDivform() {
		return divform;
	}
	public void setDivform(String divform) {
		this.divform = divform;
	}
	public String getAltform() {
		return altform;
	}
	public void setAltform(String altform) {
		this.altform = altform;
	}
}
